//异步并行钩子,所有任务执行完，再执行结束函数
//tapAsync(cb)
//tapPromise()
const { AsyncParallelHook } = require('tapable');

//回调写法 ??? 成功还好，失败有点疑惑
const callback = () => {
	console.time('callback:success:end');
	console.time('callback:error:end');
	const queue = new AsyncParallelHook(['arg1', 'arg2']);

	queue.tapAsync('nodeTask', (arg1, arg2, done) => {
		setTimeout(() => {
			console.log('callback:nodeTask', arg1, arg2);
			done();
		}, 1000);
	});
	queue.tapAsync('reactTask', (arg1, arg2, done) => {
		setTimeout(() => {
			console.log('callback:reactTask', arg1, arg2);
			done('2');
		}, 1000);
	});

	queue.callAsync(
		'111',
		'222',
		res => {
			console.log(res);
			console.timeEnd('callback:success:end');
		},
		() => {
			console.timeEnd('callback:error:end');
		}
	);
};

callback();

//Promise写法
const promise = () => {
	console.time('promise:success:end');
	console.time('promise:error:end');
	const queue = new AsyncParallelHook(['arg1', 'arg2']);

	queue.tapPromise('nodeTask', (arg1, arg2) => {
		return new Promise((resolve, reject) => {
			setTimeout(() => {
				console.log('promise:nodeTask', arg1, arg2);
				resolve();
			}, 1000);
		});
	});
	queue.tapPromise('reactTask', (arg1, arg2) => {
		return new Promise((resolve, reject) => {
			setTimeout(() => {
				console.log('promise:reactTask', arg1, arg2);
				reject();
			}, 1000);
		});
	});

	queue
		.promise('111', '222')
		.then(() => {
			console.timeEnd('promise:success:end');
		})
		.catch(() => {
			console.timeEnd('promise:error:end');
		});
};

// promise();
